package org.br.erp.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.br.erp.base.entity.Basequeryreport;
import org.br.erp.base.entity.Basequeryreportitem;
import org.br.erp.base.mapper.BasequeryreportitemMapper;
import org.br.erp.base.mapper.BasequeryreportMapper;
import org.br.erp.base.service.IBasequeryreportService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 可用库存查询详情主表
 * @Author: jeecg-boot
 * @Date:   2023-06-11
 * @Version: V1.0
 */
@Service
public class BasequeryreportServiceImpl extends ServiceImpl<BasequeryreportMapper, Basequeryreport> implements IBasequeryreportService {

	@Autowired
	private BasequeryreportMapper basequeryreportMapper;
	@Autowired
	private BasequeryreportitemMapper basequeryreportitemMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(Basequeryreport basequeryreport, List<Basequeryreportitem> basequeryreportitemList) {
		basequeryreportMapper.insert(basequeryreport);
		if(basequeryreportitemList!=null && basequeryreportitemList.size()>0) {
			for(Basequeryreportitem entity:basequeryreportitemList) {
				//外键设置
				entity.setParentid(basequeryreport.getId());
				basequeryreportitemMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(Basequeryreport basequeryreport,List<Basequeryreportitem> basequeryreportitemList) {
		basequeryreportMapper.updateById(basequeryreport);
		
		//1.先删除子表数据
		basequeryreportitemMapper.deleteByMainId(basequeryreport.getId());
		
		//2.子表数据重新插入
		if(basequeryreportitemList!=null && basequeryreportitemList.size()>0) {
			for(Basequeryreportitem entity:basequeryreportitemList) {
				//外键设置
				entity.setParentid(basequeryreport.getId());
				basequeryreportitemMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		basequeryreportitemMapper.deleteByMainId(id);
		basequeryreportMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			basequeryreportitemMapper.deleteByMainId(id.toString());
			basequeryreportMapper.deleteById(id);
		}
	}

	@Override
	public List<Basequeryreport> getBaseQueryReportByCptId(String cptid) {
		List<Basequeryreport> res=new ArrayList<>();
		QueryWrapper<Basequeryreport> queryWrapper=new QueryWrapper<>();
		queryWrapper.eq("cptid",cptid);
		queryWrapper.ne("totalqty",0);
		List<Basequeryreport> basequeryreports = basequeryreportMapper.selectList(queryWrapper);
		return basequeryreports;
	}


}
